package com.yes_iot.controller;

import com.yes_iot.util.FileInfo;
import com.yes_iot.util.FileUtil;
import com.yes_iot.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/yesiot")
@Slf4j
@CrossOrigin
public class FeilInfoController {

    @Value("${file.oss.basePath}")
    private String basePath;

    @Value("${file.oss.domain}")
    private String domain;
    @Value("${file.oss.mappingPath}")
    private String mappingPath;
    @PostMapping("/fileInfo/save")
    public Result<?> save(@RequestPart("file") MultipartFile file,
                          @RequestParam(value = "unzip", defaultValue = "0") Integer unzip, HttpServletRequest request) throws Exception {
        FileInfo fileInfo = FileUtil.getFileInfo(file);
        fileInfo = uploadFile(file,fileInfo);
        return Result.succeed(fileInfo, "上传成功");
    }

    protected FileInfo uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception {
        FileUtil.setPath(fileInfo,basePath,mappingPath,domain);
        // 本地保存文件
        FileUtil.saveFile(file, fileInfo.getPath());
        return fileInfo;
    }
}
